package com.nd.android.musicboxlibrary.util;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.view.KeyEvent;

import com.nd.android.musicboxlibrary.business.comm.MagicNumber;
import com.nd.android.musicboxlibrary.business.comm.NdException;

/**
 * 异步任务封装
 * 
 * @author 慕容秋 (muroqiu@qq.com)
 */
public abstract class NdTask extends AsyncTask<Object, Integer, Object> implements MagicNumber {
    /**
     * 
     * <br>Created 2014年11月28日 上午12:05:46
     * @author       wat.ztag
    */
    @Override
    public void initMagicNumber() {
    }
    
    protected ProgressDialog mPd = null;

    protected Context mContext;

    protected int mResID = 0;

    /**
     * @param context
     *            {当前活动}
     * @param resId
     *            {提示信息}
     */
    public NdTask(Context context, int resId) {
        super();
        mContext = context;
        mResID = resId;
        if (mContext != null && resId > 0) {
            mPd = new ProgressDialog(mContext);
            mPd.setCancelable(false);
            mPd.setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    // 屏蔽Search键(因为按搜索按键会被关闭)
                    switch (event.getKeyCode()) {
                    case KeyEvent.KEYCODE_SEARCH:
                        return true;
                    case KeyEvent.KEYCODE_BACK:
                        // cancelTask();
                        // cancelConfirm();
                        return true;
                    default:
                        return false;
                    }
                }
            });
            mPd.setMessage(mContext.getResources().getString(resId));
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (mPd != null) {
            mPd.show();
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Object result) {
        try {
            super.onPostExecute(result);
            if (!this.isCancelled()) {
                if ((mPd != null) && (mPd.isShowing())) {
                    mPd.dismiss();
                }
                if (result == null || !(result instanceof NdException)) {
                    // 成功
                    doSuccess(result);
                } else {
                    // 失败
                    doFail((NdException) result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 界面_成功处理
     * 
     * @param result Object
     */
    protected abstract void doSuccess(Object result);

    /**
     * 界面_错误处理
     * 
     * @param result NdException
     */
    protected abstract void doFail(NdException result);

    /**
     * 
     * <br>
     * Created 2014年9月11日 下午11:18:57
     * 
     * @author wat.ztag
     */
    // private void cancelTask() {
    // if ((mPd != null) && (mPd.isShowing())) {
    // mPd.dismiss();
    // }
    // cancel(true);
    // }

    /**
     * 
     * <br>
     * Created 2014年9月11日 下午11:18:59
     * 
     * @param params Object
     * @author wat.ztag
     */
    @SuppressLint("NewApi")
    public void executeByVersion(Object... params) {
        if (VERSION.SDK_INT >= n11) {
            executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else {
            execute(params);
        }
    }

}
